package com.run.guifeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.run.guifeng.dao.GoodsInfoMapper;
import com.run.guifeng.model.entity.*;
import com.run.guifeng.dao.UserMapper;
import com.run.guifeng.model.enums.ServiceResultEnum;
import com.run.guifeng.service.UserService;
import com.run.guifeng.util.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author sky
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    public User login(String loginName, String password) {
        password = MD5Util.MD5Encode(password, "utf-8");
        return userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getLoginName, loginName)
                .eq(User::getPassword, password)
                .eq(User::getDeleted, false)
                .eq(User::getLocked, false));
    }

    @Override
    public String register(User user) {
        String password = user.getPassword();
        password = MD5Util.MD5Encode(password, "utf-8");
        user.setPassword(password);
        try {
            if (save(user).equals(ServiceResultEnum.SUCCESS.getResult())) {
                return ServiceResultEnum.SUCCESS.getResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResultEnum.OPERATE_ERROR.getResult();
    }

    @Override
    public String save(User user) {
        if (userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getLoginName, user.getLoginName())) == null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        if (userMapper.insert(user) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateInfo(User user) {
        User temp = userMapper.selectById(user.getId());
        if (temp != null) {
            user.setUpdateTime(new Date());
            if (userMapper.updateById(user) > 0) {
                return ServiceResultEnum.SUCCESS.getResult();
            }
            return ServiceResultEnum.DB_ERROR.getResult();
        }
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public String deleteUser(Integer id) {
        if (userMapper.deleteById(id) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public User searchUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<User> searchUserListByContent(String content) {
        List<User> list;
        if (content == null || "".equals(content)) {
            list = userMapper.selectList(
                    null
            );
        } else {
            list = userMapper.selectList(
                    new QueryWrapper<User>()
                            .like("login_name", content)
                            .or()
                            .like("nick_name", content)
            );
        }
        return list;
    }
}
